package com.example.auth.component;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.convert.Convert;
import com.example.auth.model.domain.SysMenu;
import com.example.auth.model.domain.SysRole;
import com.example.auth.service.IRoleAndMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author xijie.wang
 * @decription:获取权限 自定义权限验证接口扩展
 * @date 2022-03-05 16:12
 */
@Component
public class RoleAndMenuImpl implements StpInterface {

	@Autowired
	private IRoleAndMenuService IRoleAndMenuService;


	/**
	 * 返回一个账号所拥有的权限码集合
	 *
	 * @param userId
	 * @param userType
	 * @return
	 */
	@Override
	public List<String> getPermissionList(Object userId, String userType) {
		List<SysMenu> menus = IRoleAndMenuService.getMenuOrPermissionList(Convert.toInt(userId), null);
		return menus.stream().map(SysMenu::getCode).collect(toList());
	}

	/**
	 * 返回一个账号所拥有的角色标识集合
	 *
	 * @param userId
	 * @param userType
	 * @return
	 */
	@Override
	public List<String> getRoleList(Object userId, String userType) {
		List<SysRole> roles = IRoleAndMenuService.getRoleList(Convert.toInt(userId));
		return roles.stream().map(SysRole::getCode).collect(toList());
	}
}
